package com.sky.controller.user;

import com.sky.dto.AddressBookDto;
import com.sky.entity.AddressBook;
import com.sky.service.AddressBookService;
import com.sky.web.controller.BaseController;
import com.sky.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "微信用户地址簿接口")
public class AddressBookController extends BaseController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public AjaxResult list() {
        List<AddressBook> list = addressBookService.selectAddressBook();
        return AjaxResult.success(list);
    }

    /**
     * 新增用户地址信息
     * @param dto 用户地址信息
     */
    @PostMapping
    @ApiOperation("新增地址")
    public AjaxResult save(@RequestBody AddressBookDto dto) {

        return toAjax(addressBookService.insertAddressBook(dto));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public AjaxResult getById(@PathVariable("id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return AjaxResult.success(addressBook);
    }

    /**
     * 查询用户默认地址
     */
    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public AjaxResult getDefault() {
        AddressBook addressBook = addressBookService.selectAddressBookByUserIdDefault();
        return AjaxResult.success(addressBook);
    }

    /**
     * 根据id修改地址
     *
     * @param dto 用户地址信息
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public AjaxResult update(@RequestBody AddressBookDto dto) {
        return toAjax(addressBookService.updateAddressBook(dto));
    }

    /**
     * 设置默认地址
     *
     * @param dto 地址信息
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public AjaxResult setDefault(@RequestBody AddressBookDto dto) {
        return toAjax(addressBookService.setDefault(dto));
    }

    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public AjaxResult deleteById(Long id) {
        return toAjax(addressBookService.deleteById(id));
    }
}
